package org.dairy.farms.common;

import org.dairy.farms.core.NandanaThreadLocal;
import org.dairy.farms.core.UserSession;
import org.dairy.farms.dao.FarmDAO;
import org.dairy.farms.dao.PersonDAO;
import org.dairy.farms.model.Farm;
import org.dairy.farms.model.Person;
import org.dairy.farms.persistence.HibernateUtil;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.SpringVersion;

import java.sql.Date;
import java.sql.Timestamp;
import java.util.List;
import java.util.Random;

public class App {
    /* public static void main( String[] args )
    {
        System.out.println("Maven + Hibernate + MySQL");
        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.getTransaction().commit();
    }*/

    ApplicationContext context = null;

    public App() {

        System.out.println("Maven + Hibernate + MySQL");
       /* Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.getTransaction().commit();*/

        System.out.println("Spring version:" + SpringVersion.getVersion());
        context = new ClassPathXmlApplicationContext(new String[]{"nandana-hibernate-config.xml"});
    }

    public Farm insertFarm(String farmName) {
        Farm farm = new Farm();
        farm.setName(farmName);
        farm.setDescription("Honesty is what we believe inn.");
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        farm.setStartDate(new Date(timestamp.getTime()));
        FarmDAO farmDAO = (FarmDAO) context.getBean("farmDao");
        farmDAO.create(farm);
        return farm;
    }

    public Person insertRootPerson(String emailAddress, Farm farm) {
        Person p = new Person();
        p.setFirstName("Root");
        p.setLastName("User");
        p.setEmailAddress((emailAddress != null && emailAddress.length() > 0) ? emailAddress : "root@gmail.com");
        p.setPassword("samba123");
        p.setFarm(farm);
        PersonDAO personDAO = (PersonDAO) context.getBean("personDao");
        personDAO.create(p);
        return p;
    }

    public Person findPerson(String emailAddress) {
        PersonDAO personDao = context.getBean("personDao", PersonDAO.class);
        List<Person> personsList = personDao.findByProperty("emailAddress", emailAddress);
        return (personsList == null || personsList.size() <= 0) ? null : personsList.get(0);
    }

    public static void main(String s[]) {
        App app = new App();
        UserSession us = new UserSession();
        Person person = app.findPerson("giridhar.duggirala@gmail.com");
        if (person == null) {
            us.setLoginName("giridhar.duggirala@gmail.com");
            us.setPersonId(1l);
            NandanaThreadLocal.set(us);
        } else {
            us.setLoginName(person.getEmailAddress());
            us.setPersonId(person.getId());
            NandanaThreadLocal.set(us);
        }
        Farm farm = app.insertFarm("Nandana Farms "+ new Random().nextInt());
        app.insertRootPerson("giridhar.duggirala@gmail.com"+new Random().nextInt(), farm);
    }
}
